package com.xiaren.ui.tree.treedrag;

// TreeDropTarget.java
// A quick DropTarget that's looking for drops from draggable JTrees.
//

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetContext;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.tree.JTreeBiz;
import com.xiaren.ui.tree.JTreeNode;

/**
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class TreeDropTarget implements DropTargetListener {

	public static JTreeNode parent = null;
	public static JTreeNode node = null;
	DropTarget target;
	JTree targetTree;

	public TreeDropTarget(JTree tree) {
		targetTree = tree;
		target = new DropTarget(targetTree, this);
	}

	/*
	 * Drop Event Handlers
	 */
	private TreeNode getNodeForEvent(DropTargetDragEvent dtde) {
		Point p = dtde.getLocation();
		DropTargetContext dtc = dtde.getDropTargetContext();
		JTree tree = (JTree) dtc.getComponent();
		TreePath path = tree.getClosestPathForLocation(p.x, p.y);
		return (TreeNode) path.getLastPathComponent();
	}

	public void dragEnter(DropTargetDragEvent dtde) {
//		System.out.println("dragEnter");
		TreeNode node = getNodeForEvent(dtde);
		// System.out.println(node.getClass());
		dtde.acceptDrag(dtde.getDropAction());
	}

	public void dragOver(DropTargetDragEvent dtde) {
		// TreeNode node = getNodeForEvent(dtde);
		// dtde.acceptDrag(dtde.getDropAction());
		Point pt = dtde.getLocation();
		DropTargetContext dtc = dtde.getDropTargetContext();
		JTree tree = (JTree) dtc.getComponent();
		TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
		parent = (JTreeNode) parentpath.getLastPathComponent();

	}

	public void dragExit(DropTargetEvent dte) {
//		System.out.println("dragExit");
	}

	public void dropActionChanged(DropTargetDragEvent dtde) {
		System.out.println("dropActionChanged");
	}

	public void drop(DropTargetDropEvent dtde) {
//		System.out.println("drop");
		Point pt = dtde.getLocation();
		DropTargetContext dtc = dtde.getDropTargetContext();
		JTree tree = (JTree) dtc.getComponent();
		TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
		JTreeNode parent = (JTreeNode) parentpath.getLastPathComponent();

		try {
			Transferable tr = dtde.getTransferable();
			DataFlavor[] flavors = tr.getTransferDataFlavors();
			for (int i = 0; i < flavors.length; i++) {
				if (tr.isDataFlavorSupported(flavors[i])) {
					dtde.acceptDrop(dtde.getDropAction());
					TreePath p = (TreePath) tr.getTransferData(flavors[i]);
					node = (JTreeNode) p.getLastPathComponent();
					if (!JTreeBiz.canTrag(parent, node)) {
//						System.out.println("不能拖动");
						JOptionPane.showMessageDialog(null, "Don't drag!");
						return;
					}
//					System.out.println("能拖动");
					DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
					model.insertNodeInto(node, parent, 0);
					dtde.dropComplete(true);
					return;
				}
			}
			dtde.rejectDrop();
		} catch (Exception e) {
			e.printStackTrace();
			dtde.rejectDrop();
		}
	}

	public static void tt() {
		new TreeDragSource(ObjectConstant.mainTree,
				DnDConstants.ACTION_COPY_OR_MOVE);
		new TreeDropTarget(ObjectConstant.mainTree);
	}
}
